/**
 * Copyright (c) 2023 - present All rights reserved.
 * <p>
 * Reprinted has been protected by the copyright laws.
 */

package cn.myxl.smart.devkit.commit.configuration;

import cn.myxl.smart.devkit.commit.model.SettingState;
import cn.myxl.smart.devkit.core.util.I18nUtil;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.DoubleClickListener;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBScrollPane;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Optional;
import cn.myxl.smart.devkit.commit.persistent.HelperSetting;

/**
 * @author alan.yang.zhang
 * @date 2023-11-14 11:49
 */

public class HelperUI {
	private JPanel helperUIPanel;
	private JPanel templateEditPanel;
	private JPanel typeDefinitionPanel;
	private JTabbedPane containerPanel;
	private JPanel headerPanel;
	private JPanel templateDefinitionPanel;

	protected HelperSetting helperSetting;
	private final CommitTypeTable commitTypeTable;
	private final Editor templateEditor;

	public HelperUI(HelperSetting helperSetting) {
		this.helperSetting = helperSetting;
		SettingState settingState = helperSetting.getSettingState();

		commitTypeTable = new CommitTypeTable(settingState);

		String template = Optional.of(settingState.getTemplate()).orElse("");

		templateEditor = initTemplatePanelUI(template);

		initTypeDefinitionPanelUI();
	}

	private void initTypeDefinitionPanelUI() {
		typeDefinitionPanel.add(ToolbarDecorator.createDecorator(commitTypeTable)
				.setAddAction(button -> commitTypeTable.handleAddAction())
				.setRemoveAction(button -> commitTypeTable.handleRemoveAction())
				.setEditAction(button -> commitTypeTable.handleEditAction())
				.setMoveUpAction(anActionButton -> commitTypeTable.handleMoveUpAction())
				.setMoveDownAction(anActionButton -> commitTypeTable.handleMoveDownAction())
				.addExtraAction(new AnActionButton("Reset Default Aliases", AllIcons.Actions.Rollback) {
					@Override
					public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
						commitTypeTable.reset();
					}
				})
				.createPanel(), BorderLayout.CENTER);
		new DoubleClickListener() {
			@Override
			protected boolean onDoubleClick(MouseEvent e) {
				return commitTypeTable.handleEditAction();
			}
		}.installOn(commitTypeTable);
	}

	@NotNull
	private Editor initTemplatePanelUI(String template) {
		EditorFactory editorFactory = EditorFactory.getInstance();

		Editor editor = editorFactory.createEditor(
				editorFactory.createDocument(""), null,
				FileTypeManager.getInstance().getFileTypeByExtension("vm"), false);

		EditorSettings editorSettings = editor.getSettings();

		editorSettings.setAdditionalLinesCount(0);
		editorSettings.setAdditionalColumnsCount(0);
		editorSettings.setLineMarkerAreaShown(false);
		editorSettings.setVirtualSpace(false);

		JBScrollPane jbScrollPane = new JBScrollPane(editor.getComponent());
		jbScrollPane.setMaximumSize(new Dimension(150, 50));

		templateEditPanel.add(jbScrollPane);

		ApplicationManager.getApplication().runWriteAction(() -> editor.getDocument().setText(template));

		return editor;
	}

	public void reset() {
		commitTypeTable.reset();

		String template = helperSetting.getSettingState().getTemplate();

		ApplicationManager.getApplication().runWriteAction(() -> templateEditor.getDocument().setText(template));
	}

	public boolean isSettingModified() {
		return commitTypeTable.isModified();
	}

	public boolean isTemplateModified() {
		return !helperSetting.getSettingState().getTemplate().equals(templateEditor.getDocument().getText());
	}

	public boolean isModified() {
		return isSettingModified() || isTemplateModified();
	}

	public JPanel getMainPanel() {
		return helperUIPanel;
	}

	public SettingState apply() {
		SettingState settingState = new SettingState();

		settingState.setTypeAliases(commitTypeTable.applyTypeAliases());
		settingState.setTemplate(templateEditor.getDocument().getText());

		return settingState;
	}
}
